package com.seal.yoyoshop.adminweb;

import com.seal.yoyoshop.bin.User;
import com.seal.yoyoshop.service.UserService;
import com.seal.yoyoshop.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "UserModifyServlet", value = "/admin/userModifyPwd")
public class UserModifyServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("flag",2);

        String idstr = request.getParameter("id");
        int id = Integer.parseInt(idstr);
        User user = userService.getUserById(id);
        request.setAttribute("user",user);

        request.getRequestDispatcher("/admin/user_modifyPwd.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String idstr = request.getParameter("id");
        int id = Integer.parseInt(idstr);
        User user = userService.getUserById(id);
        String password = request.getParameter("password");
        userService.updateUserPassword(user.getUsername(),password);
        response.sendRedirect("userList?page=1&size=16");
    }
}
